/*
 * @Author: song
 * @Description:窗口Item 绘制基类
 * @Date: 2024年04月23日 星期二 14:04:11
 * @Modify:
 */

using System.Reflection.Emit;
using UnityEditor;
using UnityEngine;

namespace SCore.Editor.WindowDraw
{
    public interface ICommonView
    {
        public abstract void UpdateEvent(Event e);
        public abstract void OnGUI();

        public abstract void OnEnable();
        public abstract void OnDisable();
        public abstract void Update();
    }

    public abstract class ECommonView : ScriptableObject, ICommonView
    {
        [Tooltip("父窗口")] protected EditorWindow _parentWindow;
        [Tooltip("父窗口")] protected IESubWindow _ieSubWindow;
        [Tooltip("区域")] protected Rect _rect;
        [Tooltip("是否按下")] private bool _down;


        //============>Lift
        public virtual void UpdateEvent(Event e)
        {
            HandleEvent(e.type, e);
        }

        public abstract void OnGUI();

        public virtual void Update()
        {
        }

        public virtual void OnEnable()
        {
        }

        public virtual void OnDisable()
        {
        }

        //============>Event
        protected virtual bool MouseDown(Event evt)
        {
            return false;
        }

        protected virtual bool MouseDrag(Event evt)
        {
            return false;
        }

        protected virtual bool MouseWheel(Event evt)
        {
            return false;
        }

        protected virtual bool MouseUp(Event evt)
        {
            return false;
        }

        protected virtual bool DoubleClick(Event evt)
        {
            return false;
        }

        protected virtual bool KeyDown(Event evt)
        {
            return false;
        }

        protected virtual bool KeyUp(Event evt)
        {
            return false;
        }

        protected virtual Vector2 GetMousePos(Event evt)
        {
            return evt.mousePosition;
        }

        bool HandleEvent(EventType type, Event evt)
        {
            bool isHandled = false;

            switch (type)
            {
                case EventType.ScrollWheel:
                    if (_rect.Contains(GetMousePos(evt)))
                    {
                        isHandled = MouseWheel(evt);
                    }

                    break;
                case EventType.MouseUp:
                    if (_down)
                    {
                        _down = false;
                        isHandled = MouseUp(evt);
                    }

                    break;
                case EventType.MouseDown:
                    if (_rect.Contains(GetMousePos(evt)))
                    {
                        _down = true;
                        isHandled = evt.clickCount < 2 ? MouseDown(evt) : DoubleClick(evt);
                    }

                    break;
                case EventType.MouseDrag:
                    if (_down)
                    {
                        isHandled = MouseDrag(evt);
                    }

                    break;
                case EventType.KeyDown:
                    isHandled = KeyDown(evt);
                    break;
                case EventType.KeyUp:
                    isHandled = KeyUp(evt);
                    break;
            }

            if (isHandled)
                evt.Use();

            return isHandled;
        }


        // public
        public void UpdateArea(Rect rect)
        {
            _rect = rect;
        }

        public void SetUp(EditorWindow editorWindow)
        {
            _parentWindow = editorWindow;
        }

        public void SetUp(IESubWindow editorWindow)
        {
            _ieSubWindow = editorWindow;
        }
    }

    public abstract class NCommonView : ICommonView
    {
        [Tooltip("父窗口")] protected EditorWindow _parentWindow;
        [Tooltip("父窗口")] protected IESubWindow _ieSubWindow;
        [Tooltip("区域")] protected Rect _rect;
        [Tooltip("是否按下鼠标任何按键")] public bool _down;

        //============>Lift
        public virtual void UpdateEvent(Event e)
        {
            HandleEvent(e.type, e);
        }

        public virtual void OnGUI()
        {
        }

        public virtual void Update()
        {
        }

        public virtual void OnEnable()
        {
        }

        public virtual void OnDisable()
        {
        }

        //============>Event
        protected virtual bool MouseDown(Event evt)
        {
            return false;
        }

        protected virtual bool MouseDrag(Event evt)
        {
            return false;
        }

        protected virtual bool MouseWheel(Event evt)
        {
            return false;
        }

        protected virtual bool MouseUp(Event evt)
        {
            return false;
        }

        protected virtual bool DoubleClick(Event evt)
        {
            return false;
        }

        protected virtual bool KeyDown(Event evt)
        {
            return false;
        }

        protected virtual bool KeyUp(Event evt)
        {
            return false;
        }

        protected virtual bool ContextClick(Event evt)
        {
            return false;
        }

        protected virtual Vector2 GetMousePos(Event evt)
        {
            return evt.mousePosition;
        }

        bool HandleEvent(EventType type, Event evt)
        {
            bool isHandled = false;

            switch (type)
            {
                case EventType.ScrollWheel:
                    if (_rect.Contains(GetMousePos(evt)))
                    {
                        isHandled = MouseWheel(evt);
                    }

                    break;
                case EventType.MouseUp:
                    if (_down)
                    {
                        isHandled = MouseUp(evt);
                        _down = false;
                    }

                    break;
                case EventType.MouseDown:
                    if (_rect.Contains(GetMousePos(evt)))
                    {
                        _down = true;
                        isHandled = evt.clickCount < 2 ? MouseDown(evt) : DoubleClick(evt);
                    }

                    break;
                case EventType.MouseDrag:
                    if (_down)
                    {
                        isHandled = MouseDrag(evt);
                    }

                    break;
                case EventType.KeyDown:
                    isHandled = KeyDown(evt);
                    break;
                case EventType.KeyUp:
                    isHandled = KeyUp(evt);
                    break;
                case EventType.ContextClick:
                    if (_rect.Contains(GetMousePos(evt)))
                    {
                        isHandled = ContextClick(evt);
                    }

                    break;
            }

            if (isHandled)
                evt.Use();

            return isHandled;
        }


        // public
        public void UpdateArea(Rect rect)
        {
            _rect = rect;
        }

        public Rect GetRect()
        {
            return _rect;
        }

        public void SetUp(EditorWindow editorWindow)
        {
            _parentWindow = editorWindow;
        }

        public void SetUp(IESubWindow editorWindow)
        {
            _ieSubWindow = editorWindow;
        }

        public T GetSubEditorWindow<T>() where T : IESubWindow
        {
            return _ieSubWindow as T;
        }

        public T GetEditorWindow<T>() where T : EditorWindow
        {
            return _parentWindow as T;
        }
    }
}